/*
 * D.A.O. for JPA  
 * Created on 6 juin 2013 ( Time 15:03:46 )
 */

package org.demo.pointeuse.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.telosys.starterkits.strutsjpa.CoreUtils;
import org.telosys.starterkits.strutsjpa.GenericJpaDao;

import org.demo.pointeuse.bean.Pointeuse;

public class JpaPointeuseDAO extends GenericJpaDao<Pointeuse, Integer> {

	private static final Predicate[] ap = {};
	
	/**
	 * Constructs a DAO for Pointeuse
	 */
	public JpaPointeuseDAO() {
		super(Pointeuse.class);
	}
	
	/**
	 * Search by Pointeuse example
	 */
	public List<Pointeuse> search(final Pointeuse element) {
		final EntityManager em = getEntityManager();
		if ( element != null  ) {
			CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
			CriteriaQuery<Pointeuse> criteriaQuery = criteriaBuilder.createQuery(Pointeuse.class);
			Root<Pointeuse> from = criteriaQuery.from(Pointeuse.class);

			List<Predicate> listP = new ArrayList<Predicate>();


			if (CoreUtils.isNotBlank(element.getIdpointeuse())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idpointeuse"), element.getIdpointeuse());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdsite())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idsite"), element.getIdsite());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdpointage())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idpointage"), element.getIdpointage());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getLocalisation())) {
				Predicate predicate = criteriaBuilder.equal(from.get("localisation"), element.getLocalisation());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getPorte())) {
				Predicate predicate = criteriaBuilder.equal(from.get("porte"), element.getPorte());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getAddip())) {
				Predicate predicate = criteriaBuilder.equal(from.get("addip"), element.getAddip());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getAddmac())) {
				Predicate predicate = criteriaBuilder.equal(from.get("addmac"), element.getAddmac());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getPort())) {
				Predicate predicate = criteriaBuilder.equal(from.get("port"), element.getPort());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getCommunication())) {
				Predicate predicate = criteriaBuilder.equal(from.get("communication"), element.getCommunication());
				listP.add(predicate);
			}
			
			criteriaQuery.where(listP.toArray(ap));
			TypedQuery<Pointeuse> tq = em.createQuery(criteriaQuery);
			return tq.getResultList();
		}
		else
		{
    		return this.loadAll();
		}
	}

}